package cn.com.hhrcw.job;

import cn.com.hhrcw.entity.OpInterviewHistory;
import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.service.HuaWeiRoomService;
import cn.com.hhrcw.service.IOpInterviewHistoryService;
import cn.com.hhrcw.service.IPiInfoService;
import cn.com.hhrcw.utils.RedisLockUtils;
import cn.com.hhrcw.utils.huawei.CreateMeetingParam;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.RedisUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @ProjectName: recruit-boot-parent
 * @Package: cn.com.hhrcw.job
 * @ClassName: CloseInterviewTask
 * @Author: 朱修轩
 * @Description: 视频面试定时任务 关闭面试房间
 * @Date: 2021/5/20 16:04
 * @Version: 1.0
 */
@Component("CloseInterviewTask")
@Slf4j
public class CloseInterviewTask implements Job {
    @Autowired
    private IOpInterviewHistoryService opInterviewHistoryService;

    @Autowired
    private HuaWeiRoomService huaWeiRoomService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtil redisUtil;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        DateTime date = DateUtil.date();
        // 关闭到时间未接受面试的定时任务
        opInterviewHistoryService.lambdaUpdate().set(OpInterviewHistory::getOnLineState,1)
                .le(OpInterviewHistory::getTime,date)
                .eq(OpInterviewHistory::getOutLineState,0)
                .eq(OpInterviewHistory::getOnLineState,0)
                .update();



        // 关闭会议室定时任务
        Set<Object> closeSet = redisUtil.zRange("closeMeeting", 0, 0);
        Object[] closeArr = closeSet.toArray();
        if(closeArr.length > 0){
            Object id = closeArr[0];
            //防重复执行
            String closeMeeting = "closeMeeting";
            String locked = "_locked";
            if (redisTemplate.hasKey(closeMeeting+ id +locked)) {
                log.warn("关闭" + id + "任务执行中...");
                return;
            }
            int time1 = 120;
            if (RedisLockUtils.tryLock(closeMeeting + id, time1)) {
                try {


                    Double time = redisUtil.zScore("closeMeeting", id);

                    DateTime closeDate = DateUtil.date(time.longValue());
                    log.info("------------------------关闭面试房间时间" + DateUtil.format(closeDate,"yyyy-MM-dd HH:mm:ss")
                    + "当前时间"+ DateUtil.format(date,"yyyy-MM-dd HH:mm:ss"));
                    if (DateUtil.compare(date,closeDate) >= 0){
                        redisUtil.zRemove("closeMeeting", id);
                        OpInterviewHistory opInterviewHistory = opInterviewHistoryService.getById(id.toString());
                        String r3 = opInterviewHistory.getR3();

                        huaWeiRoomService.cancelMeeting(r3);
                        log.info(opInterviewHistory.getUserId() +"的视频面试房间关闭");
                        opInterviewHistoryService.lambdaUpdate().set(OpInterviewHistory::getOnLineState,1)
                                .eq(OpInterviewHistory::getId,opInterviewHistory.getId()).update();
                        // 释放云会议室
                        redisUtil.del("inuseMeeting:" + opInterviewHistory.getR2());
                    }
                }catch (Exception e){
                    log.error("关闭面试房间错误" + e.getMessage(), e);
                }finally {
                    RedisLockUtils.unLock("closeMeeting" + id);
                }
            }
        }

    }
}
